package com.dgut.legalservice.controller;



import com.dgut.legalservice.domain.Msg;
import com.dgut.legalservice.domain.User;
import com.dgut.legalservice.service.UserService;
import com.dgut.legalservice.vCode.FileUpLoad;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Logger;

@Controller
@RequestMapping("/center")
public class NormalUserController {    //普通用户信息管理控制层  made by yefuda

    @Autowired
    private UserService userServiceiml;



    @RequestMapping(value = "/updateBase", method = RequestMethod.POST) //修改基本信息
//    public String updateBase(User user,String birth,HttpSession session, MultipartFile photoAtt, HttpServletRequest request) throws IOException, ParseException {
    public String updateBase(User user,String birth,HttpSession session, HttpServletRequest request) throws IOException, ParseException {
        User user1 = (User) session.getAttribute("user");
        FileUpLoad file = new FileUpLoad();
        LocalDate localDate = LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        String name = photoAtt.getOriginalFilename();
//        if(!name.equals("")){   //不传头像的情况
//            user1.setPhotoAttId(file.fileUpload(photoAtt, request,session));  //设置头像url
//        }
        user1.setRealName(user.getRealName());
        user1.setSex(user.getSex());
        user1.setEmail(user.getEmail());
        user1.setAddress(user.getAddress());
        user1.setQuestion(user.getQuestion());
        user1.setAnswer(user.getAnswer());
        user1.setBirthday(localDate);
        user1.setPostNum(user.getPostNum());
        user1.setIdCardNum(user.getIdCardNum());
        user1.setPhoneNum(user.getPhoneNum());
        user1.setIntro(user.getIntro());

        userServiceiml.updateNorUser(user1);//更新t_sys_user表
        session.setAttribute("user",user1);  //更新session

        return "/user/user-info";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(String newPassword,HttpSession session){
        Msg msg = new Msg();
        User user = (User) session.getAttribute("user");
        user.setPassword(newPassword);
        userServiceiml.updatePassword(user);
        if (user.getType().equals("普通用户"))
            return "/user/user-info";
        else if (user.getType().equals("超级管理员"))
            return "/platform/user-info";
        else
            return "/provider/user-info";
    }


    @RequestMapping("/checkPassword")
    @ResponseBody
    public Msg checkPassword(HttpSession session,String oldPassword){
        Msg msg = new Msg();
        User user = (User) session.getAttribute("user");
        String checkPassword = user.getPassword();
        if (checkPassword.equals(oldPassword))
            msg.setCode(200);  //原密码正确
        else if(!checkPassword.equals(oldPassword)){
            msg.setCode(100);  //100-密码错误
        }
        return msg;
    }

    @RequestMapping("/company-info")
    public String company_info(){
        return "/provider/company-info";
    }

    @RequestMapping("/updateCompany-info")
    public String updateCompany(HttpSession session,User user){
        User user1 = (User) session.getAttribute("user");
        user1.setLawyerNum(user.getLawyerNum());
        user1.setTelNum(user.getTelNum());
        user1.setLegalOrg(user.getLegalOrg());
        user1.setNowPosition(user.getNowPosition());
        user1.setServiceYear(user.getServiceYear());
        userServiceiml.updateCompanyInfo(user1);
        session.setAttribute("user",user1);
        return "/provider/company-info";
    }



}
